<template>
  <div>
    <el-card class="el-card" shadow="hover" :body-style="{ padding: '0px' }">
      <!-- 添加数码类别弹出框 -->
      <el-dialog title="添加数码类别" :visible.sync="dialogVisible" width="50%" :before-close="handleClose">

        <el-form
          :model="ruleForm"
          :hide-required-asterisk="true"
          :status-icon="true"
          :rules="rules"
          ref="ruleForm"
        >
          <el-form-item label="类型名称" prop="name" :label-width="formLabelWidth">
            <el-input class="inp1" v-model="ruleForm.name"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
          <el-button type="primary" @click="addDigitalType()" size="mini">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 编辑弹出框 -->
<el-dialog title="编辑" :visible.sync="changeDialogVisible" width="50%" :before-close="handleClose">
        <el-form
          :model="ruleForm"
          :hide-required-asterisk="true"
          :status-icon="true"
          :rules="rules"
          ref="ruleForm"
        >
          <el-form-item label="类型ID"  :label-width="formLabelWidth">
            <el-input class="inp1" v-model="ruleForm.id" :disabled="true"></el-input>
          </el-form-item>

           <el-form-item label="类型名称" prop="changeName" :label-width="formLabelWidth">
            <el-input class="inp1" v-model="ruleForm.changeName" ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="changeDialogVisible = false" size="mini">取 消</el-button>
          <el-button type="primary" @click="updateName()" size="mini">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 删除弹出框 -->
      <el-dialog
        title="确定删除此数码类别？"
        :visible.sync="delDialogVisible"
        width="50%"
        :before-close="handleClose"
      >
        <el-form
          :model="ruleForm"
          :hide-required-asterisk="true"
          :status-icon="true"
          :rules="rules"
          ref="ruleForm"
        >
          <el-form-item label="类别id" prop="id" :label-width="formLabelWidth">
            <el-input class="inp1" v-model="ruleForm.delId" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="类别名称" prop="user" :label-width="formLabelWidth">
            <el-input class="inp1" v-model="ruleForm.delName" :disabled="true"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="delDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="delName()">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 搜索 -->
      <el-input
        class="el-input"
        placeholder="请输入要查询的用户"
        prefix-icon="el-icon-search"
        v-model="msg"
        clearable
      ></el-input>
      <!-- 添加 -->
      <el-button type="primary" class="bnt1" @click="dialogVisible = true">添加数码类别</el-button>
      <!-- 表格 -->
      <el-table :data="search" style="width: 100%" border>
        <!-- 表头 -->
        <el-table-column prop="digital_category_id" label="数码类别Id" width="180" header-align="center"
          align="center"></el-table-column>
        <el-table-column prop="digital_category_name" label="数码类别名字" width="180" header-align="center"
          align="center"></el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作" header-align="center"
          align="center">
          <template slot-scope="scope">
            <el-button type="success" plain size="mini"  @click="changeDigitalCategory(scope.$index, scope.row)" >修改</el-button>
             <el-button type="danger" plain size="mini" @click="delDigitalCategory(scope.$index, scope.row)">删除</el-button>
        
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>
<script>
import axios from 'axios';
import util from '../lib/index.js'
export default {
  data() {
    var validateName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入名称'));
      }
      else {
        callback();
      }
    };
    var validateChangeName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('不能为空名称'));
      }
      /* ！！！！输入什么都是string */
      //       else if(typeof(value)!=='string'){
      //  callback(new Error('输入的名称只能为中文'));
      //       }
      else {
        callback();
      }
    };
    return {
      list: [],
      msg: '',
      dialogVisible: false,
      changeDialogVisible: false,
      delDialogVisible: false,
      text: '',
      formLabelWidth: '120px',
      ruleForm: {
        name: '',
        changeName: '',
        id: '',
        delId: '',
        delName: '',
      },
      rules: {
        name: [{ validator: validateName, trigger: 'blur' }],
        changeName: [{ validator: validateChangeName, trigger: 'blur' }],
        id: [{ trigger: 'blur' }]
      },
    };
  },
  mounted() {
   util.typeAllInfor({}).then((res)=>{
  //   console.log(res.data.data);
  this.list = res.data.data;

   })
  },
  computed: {
    search() {
      let arr = [];
      for (let i = 0; i < this.list.length; i++) {
        if (
          this.list[i].digital_category_id + ''.indexOf(this.msg) !==-1 ||
          this.list[i].digital_category_name .indexOf(this.msg) !==-1
        ) {
          arr.push({
            digital_category_name: this.list[i].digital_category_name,
            digital_category_id: this.list[i].digital_category_id,
          });
        } else if (this.msg === '') {
          arr = this.list;
        }
      }
      return arr;
    },
  },
  methods: {
    //   添加数码类别
    addDigitalType() {
      this.$refs.ruleForm.validate((valid) => {
        console.log(this.ruleForm.name);
        if (valid) {
         util.typeAddInfor({
  digital_category_name: this.ruleForm.name + '',
}).then((res)=>{
   console.log(res);
   util.typeAllInfor({}).then((res)=>{
  //   console.log(res.data.data);
  this.list = res.data.data;

   })
})
          this.dialogVisible = false;
          this.ruleForm.name = '';
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 编辑弹出框
    changeDigitalCategory(item, row) {
      // console.log('item', item);
      // console.log('row', row);
      this.ruleForm.id = row.digital_category_id;
      this.ruleForm.changeName = row.digital_category_name;
      this.changeDialogVisible = true;
    },
    //编辑
    updateName() {
   this.$refs.ruleForm.validate((valid) => {
        if (valid) {
    util.typeChangeInfor({
  digital_category_id: this.ruleForm.id,
  digital_category_name: this.ruleForm.changeName,
}).then((r)=>{
 util.typeAllInfor({}).then((res)=>{
  //   console.log(res.data.data);
  this.list = res.data.data;

   })
})

          this.changeDialogVisible = false;
          this.ruleForm.changeName = '';
        } else {
          console.log('error submit!!');
          return false;
        }
      })
      // console.log(this.ruleForm.changeName);
      // console.log(this.ruleForm.id);
    },
    //删除弹出框
    delDigitalCategory(item, row) {
      this.delDialogVisible = true;
      this.ruleForm.delId = row.digital_category_id;
      this.ruleForm.delName = row.digital_category_name;
    },
    //删除
    delName() {
     util.typeDelInfor({
  digital_category_id: this.ruleForm.delId,
}).then((r)=>{
    util.typeAllInfor({}).then((res)=>{
  //   console.log(res.data.data);
  this.list = res.data.data;

   })
})

        this.$notify.error({
          title: '删除',
          message: '删除成功',
          
        });
      this.delDialogVisible = false;
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
  },
};
</script>
<style scoped>
.el-input {
  width: 400px;
  margin-bottom: 50px;
}
.el-card {
  margin-left: 86px;
  margin-top: 20px;
  padding: 10px 20px 10px;
  width: 700px;
   margin-bottom: 50px;
}
.bnt1 {
  margin-left: 98px;
}
.addDigitalTypeName {
  width: 200px;
  margin-left: 40px;
}
.inputName {
  position: absolute;
  left: 63px;
  top: 25px;
}
</style>